package me.passtime.projt.account;

import android.content.Context;
import android.content.Intent;
import me.passtime.projt.Main;
import me.passtime.projt.http.AbstractHttpController;
import me.passtime.projt.http.UriConstants;
import me.passtime.projt.reg.RegisterStepTwoPage;
import me.passtime.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-13
 * Time: 下午3:47
 */
public class RetrievePasswordController extends AbstractHttpController<String> {
    private static final String SEND = "SEND";
    private static final String SUBMIT_ONE = "SUBMIT_ONE";
    private static final String SUBMIT_TWO = "SUBMIT_TWO";
    private static final String SUBMIT_THREE = "SUBMIT_THREE";

    private RetrievePasswordStepOnePage stepOnePage;
    private RetrievePasswordStepTwoPage stepTwoPage;
    private RetrievePasswordStepThreePage stepThreePage;
    private int step = 1;
    private String actionType;

    private String regId;
    private String code;
    private String msgCode;

    public RetrievePasswordController(RetrievePasswordStepOnePage stepOnePage) {
        super(String.class);
        this.stepOnePage = stepOnePage;
        step = 1;
    }

    public RetrievePasswordController(RetrievePasswordStepTwoPage stepTwoPage) {
        super(String.class);
        this.stepTwoPage = stepTwoPage;
        step = 2;
    }

    public RetrievePasswordController(RetrievePasswordStepThreePage stepThreePage) {
        super(String.class);
        this.stepThreePage = stepThreePage;
        step = 3;
    }

    public void sendVerificationCode(String regId, String code) {
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.SEND_SMS_VERIFY_CODE).setTelNo(regId).createXml();
        this.actionType = SEND;
        this.sendRequest(UriConstants.URL, postXml);

    }

//    public void submitUserInfo(String regId, String code, String msgCode) {
//        this.regId = regId;
//        this.code = code;
//        this.msgCode = msgCode;
//        String postXml = XmlBuilder.getInstance().buildXml();
//        this.actionType = SUBMIT_ONE;
//        this.sendRequest(UriConstants.URL, postXml);
//
//    }

//    public void resetPassword(String regId, String code, String msgCode) {
//        this.regId = regId;
//        this.code = code;
//        this.msgCode = msgCode;
//        String postXml = XmlBuilder.getInstance().buildXml();
//        this.actionType = SUBMIT_TWO;
//        this.sendRequest(UriConstants.URL, postXml);
//
//    }

    public void submitResetPassword(String regId,String password,String msgCode){
        this.regId=regId;
        String  svcCont = "<SvcCont><TelNo>" + regId + "</TelNo><UserPas>" + password + "</UserPas>" + "<VerificationCode>" + msgCode + "</VerificationCode></SvcCont>";
        
        String  postXml=new PostXmlBuilder().setBiPCode(RequestCode.RESET_PASSWORD).setTelNo(regId).setSvcCont(svcCont).createXml();
        this.actionType = SUBMIT_THREE;
        this.sendRequest(UriConstants.URL, postXml);
    }


    @Override
    public void onRequestFinish(String response) {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            if (SEND.equals(actionType)) {
                if ("0000".equals(response)) {
                    ToastUtils.showMessage(stepOnePage, "验证码已经发送.");
                } else {
                    ToastUtils.showMessage(stepOnePage, "验证码发送失败.");
                }
            }
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            if (SEND.equals(actionType)) {
                if ("0000".equals(response)) {
                    ToastUtils.showMessage(stepTwoPage, "验证码已经发送.");
                } else {
                    ToastUtils.showMessage(stepTwoPage, "验证码发送失败.");
                }
            }
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            if (SUBMIT_THREE.equals(actionType)) {
                if ("0000".equals(response)) {
                    ToastUtils.showMessage(stepTwoPage, "重置密码成功.");
                    PageJumpUtils.jump(stepThreePage, Main.class);
                } else {
                    ToastUtils.showMessage(stepTwoPage, "重置密码失败.");
                }
            }
        }
    }

    @Override
    public void onNetworkAbort() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，网络连接被中断.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，网络连接被中断.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，网络连接被中断.");
        }
    }

    @Override
    public void onNetworkError() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，网络连接错误.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，网络连接错误.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，网络连接错误.");
        }
    }

    @Override
    public void onResponseError() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，返回结果错误.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，返回结果错误.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，返回结果错误.");
        }
    }
}
